package com.example.sls.dao.field;

import com.example.sls.pojo.field.FieldMaintenance;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 场地维护记录表(FieldMaintenance)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:08
 */
public interface FieldMaintenanceDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from field_maintenance where id=#{id}")
  FieldMaintenance queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param fieldMaintenance 查询条件
   * @param pageSize         分页对象
   * @param offset           偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from field_maintenance where 1=1" +
          " and (id=#{fieldMaintenance.id} or (isnull(#{fieldMaintenance.id}) or #{fieldMaintenance.id}=0))" +
          " and (field_id=#{fieldMaintenance.fieldId} or (isnull(#{fieldMaintenance.fieldId}) or #{fieldMaintenance.fieldId}=0))" +
          " and (maintenance_date=#{fieldMaintenance.maintenanceDate} or (isnull(#{fieldMaintenance.maintenanceDate}) or #{fieldMaintenance.maintenanceDate}=''))" +
          " and (details=#{fieldMaintenance.details} or (isnull(#{fieldMaintenance.details}) or #{fieldMaintenance.details}=''))" +
          " and (responsible_person=#{fieldMaintenance.responsiblePerson} or (isnull(#{fieldMaintenance.responsiblePerson}) or #{fieldMaintenance.responsiblePerson}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<FieldMaintenance> queryAllByLimit(FieldMaintenance fieldMaintenance, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from field_maintenance")
  long count();

  /**
   * 新增数据
   *
   * @param fieldMaintenance 实例对象
   * @return 影响行数
   */
  @Insert("insert into field_maintenance (id,field_id,maintenance_date,details,responsible_person) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{fieldId}!=0 and !isnull(#{fieldId}),#{fieldId},null)," +
          "if(#{maintenanceDate}!='' and !isnull(#{maintenanceDate}),#{maintenanceDate},null)," +
          "if(#{details}!='' and !isnull(#{details}),#{details},null)," +
          "if(#{responsiblePerson}!='' and !isnull(#{responsiblePerson}),#{responsiblePerson},null)" +
          ")")
  int insert(FieldMaintenance fieldMaintenance);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param fieldMaintenance 实例对象
   * @return 影响行数
   */
  @Update("update field_maintenance set " +
          "field_id=if(isnull(#{fieldId}) or #{fieldId}=0,field_id,#{fieldId})," +
          "maintenance_date=if(isnull(#{maintenanceDate}) or #{maintenanceDate}='',maintenance_date,#{maintenanceDate})," +
          "details=if(isnull(#{details}) or #{details}='',details,#{details})," +
          "responsible_person=if(isnull(#{responsiblePerson}) or #{responsiblePerson}='',responsible_person,#{responsiblePerson})" +
          " where id=#{id}")
  int update(FieldMaintenance fieldMaintenance);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from field_maintenance where id=#{id}")
  int deleteById(Integer id);
}

